package com.opensource.component.register;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

/**
 * @author ZonLen since on 2022/5/31 下午3:47
 */
public class RegisterClientInterface implements ApplicationRunner {

  private final RegisterStatementRpcClient registerStatementRpcClient;

  public RegisterClientInterface(
      RegisterStatementRpcClient registerStatementRpcClient) {
    this.registerStatementRpcClient = registerStatementRpcClient;
  }

  @Override
  public void run(ApplicationArguments args) throws Exception {
    final Map<String, RegisterInterfaceStatement> clientRegisterInterfaceStatement = RegisterStatementManager
        .clientRegisterInterfaceStatement();
    if (clientRegisterInterfaceStatement.isEmpty()) {
      return;
    }
    Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
      //注册到服务端
      registerStatementRpcClient
          .registerInterfaceStatements(new ArrayList<>(clientRegisterInterfaceStatement.values()));
    }, 30, 30, TimeUnit.SECONDS);
  }
}